package org.example.corpus.monolingual.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.corpus.common.ResponseResult;
import org.example.corpus.monolingual.controller.dto.FrequencyDto;
import org.example.corpus.monolingual.controller.dto.kwic.KwicRequest;
import org.example.corpus.monolingual.controller.dto.kwic.KwicResponse;
import org.example.corpus.monolingual.service.MonolingualService;
import org.example.corpus.monolingual.service.WordFrequencyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/corpus/mono")
@Component
@Slf4j
public class MonoCorpusController {
    @Autowired
    private MonolingualService monolingualService;
    @Autowired
    private WordFrequencyService wordFrequencyService;

    // KWIC检索词在语料库中的前后文检索（KWIC）接口
//    @PostMapping("/kwic")
//    public ResponseEntity<?> doKwic(@RequestBody KwicRequest request) {
//
//        List<KwicResponse.KwicData> results = monolingualService.getKwicResults(request);
//
//        KwicResponse response = new KwicResponse();
//        response.setCode(0);
//        response.setMessage("操作成功");
//        response.setData(results);
//
//        return ResponseEntity.ok(response);
//    }

    @PostMapping("/kwic")
    public ResponseEntity<?> doKwic_1(@RequestBody KwicRequest request) {

        List<KwicResponse.KwicData> results = monolingualService.getKwicResults(request);

        KwicResponse response = new KwicResponse();
        response.setCode(0);
        response.setMessage("操作成功");
        response.setData(results);

        return ResponseEntity.ok(response);
    }

//    // 自动识别检索词的语义并展示不同词义的检索行
//    @PostMapping("/semantic")
//    public ResponseEntity<?> doSemantic(@RequestBody ) {
//
//    }
//
//    // 自动识别检索词的句法结构并展示检索行
//    @PostMapping("/syntax")
//    public ResponseEntity<?> doSyntax(@RequestBody ) {
//
//    }
//
//    // 自动识别检索行的语义倾向并展示检索行
//    @PostMapping("/sentiment")
//    public ResponseEntity<?> doSentiment(@RequestBody ) {
//
//    }

    @GetMapping("/frequency")
    public ResponseResult doFrequency(@RequestParam String keyword,@RequestParam Integer corpusId) {
        FrequencyDto frequencyDto = wordFrequencyService.getFrequency(keyword,corpusId);
        if (frequencyDto != null)
            return ResponseResult.success(frequencyDto,"获取成功");
        return ResponseResult.error("异常！");
    }

    @GetMapping("/sort")
    public ResponseEntity<ResponseResult> doSort(
            @RequestParam Integer corpusId,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(defaultValue = "1") Integer pageNumber) {
        try {
            // 调用服务层方法获取排序后的词频数据
            List<Map<String, Object>> result = wordFrequencyService.getFrequencyRank(corpusId);

            // 进行分页处理
            int fromIndex = (pageNumber - 1) * pageSize;
            int toIndex = Math.min(fromIndex + pageSize, result.size());
            List<Map<String, Object>> paginatedResult = result.subList(fromIndex, toIndex);

            // 创建 ResponseResult 对象，封装数据
            ResponseResult response = ResponseResult.success(paginatedResult, "排序成功");

            // 返回 ResponseEntity，携带 JSON 数据以及 200 状态码
            return ResponseEntity.ok(response);
        } catch (IOException e) {
            // 异常发生时，返回 500 错误并提供错误信息
            ResponseResult response = ResponseResult.error("异常！");
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }




}